package cn.krund.lyc.hotlerobot.wifimanage;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.krund.lyc.hotlerobot.activity.ui.WifiFunctionActivity;

/**
 * Created by Office on 2015/12/1.
 */
public class PutInfoToMap {
    public static String SceneList2String(Map<String, WifiUserInfo> hashmap)
            throws IOException {
        // 实例化一个ByteArrayOutputStream对象，用来装载压缩后的字节文件。
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        // 然后将得到的字符数据装载到ObjectOutputStream
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                byteArrayOutputStream);
        // writeObject 方法负责写入特定类的对象的状态，以便相应的 readObject 方法可以还原它

        List<WifiUserInfo> list = new ArrayList<>();
        for (Map.Entry<String, WifiUserInfo> user : hashmap.entrySet()
                ) {
            list.add(user.getValue());
        }
        objectOutputStream.writeObject(list);
        // 最后，用Base64.encode将字节文件转换成Base64编码保存在String中
        String SceneListString = new String(Base64.encode(
                byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
        // 关闭objectOutputStream
        objectOutputStream.close();
        return SceneListString;
    }

    @SuppressWarnings("unchecked")
    public static Map<String, WifiUserInfo> String2SceneList(
            String SceneListString) throws StreamCorruptedException,
            IOException, ClassNotFoundException {
        byte[] mobileBytes = Base64.decode(SceneListString.getBytes(),
                Base64.DEFAULT);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                mobileBytes);
        ObjectInputStream objectInputStream = new ObjectInputStream(
                byteArrayInputStream);
        List<WifiUserInfo> SceneList = (List<WifiUserInfo>) objectInputStream
                .readObject();
        objectInputStream.close();
        Map<String, WifiUserInfo> map = new HashMap<>();
        for (WifiUserInfo wifi : SceneList
                ) {
            map.put(wifi.getName(), wifi);
        }
        return map;
    }

    public static boolean putMap(Context context, String key, Map<String, WifiUserInfo> hashmap) {
        SharedPreferences settings = context.getSharedPreferences(
                "info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        try {
            Map<String, WifiUserInfo> oldMap = getMap(context, key);
            if (WifiStateDialog.delete) oldMap = hashmap;
            if (WifiFunctionActivity.delete) oldMap = hashmap;
            if (oldMap != null && oldMap.size() > 0) {
                hashmap.putAll(oldMap);
            }
            String liststr = SceneList2String(hashmap);
            editor.putString(key, liststr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return editor.commit();
    }

    public static Map<String, WifiUserInfo> getMap(Context context, String key) {
        SharedPreferences settings = context.getSharedPreferences(
                "info", Context.MODE_PRIVATE);
        String liststr = settings.getString(key, "");
        try {
            return String2SceneList(liststr);
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
